from pyeasyaccept.PythonEasyAcceptFacade import PythonEasyAcceptFacade
from pyeasyaccept.commom.Configuration import Configuration
from pythoneasyaccept_test.ListenerClass import ListenerClass
from pyeasyaccept.script.ParsedLineReader import ParsedLineReader
from pyeasyaccept.script.Script import Script

import unittest
class ObserverTest(unittest.TestCase):
    """
        Tests if a class receives events when it is a listener from scripts tests.
        @author: Mikaela Maia
    """
   
    def setUp(self):
        """
        This method sets initial information
        """
        self.listenerClass = ListenerClass()
        self.configuration = Configuration.getInstance()
        self.FILE_SEPARATOR = Configuration.FILE_SEPARATOR          
        self.root = self.configuration.getProjectRoot()
        
    def tearDown(self):
        """
        This method tear down informations
        """
        pass

    def testObserver(self):
        """
        This method runs observer with error event
        """
        testError1 = self.root + self.configuration.ACCEPTANCE_TEST_SCRIPTS_LOCATION + self.FILE_SEPARATOR+"testError1.txt"
        
        parsedLineReader1 = ParsedLineReader(testError1)
        
        scripts = [testError1]
    
        peaFacade = PythonEasyAcceptFacade(self.listenerClass,scripts)
        peaFacade.addErrorListener(self.listenerClass)
        peaFacade.executeTests()
         
        script1 = Script(self.listenerClass,testError1)
        
        parsedLine1 = parsedLineReader1.getParsedLine()
        resultLine1 = script1.executeCommand(parsedLine1)
            
        self.assertEquals(Configuration.ERROR_CODE, resultLine1.getTestLineStatus())

        self.assertEquals('Was expected: 1, but got: 2 ', self.listenerClass.getMessage())

    def testObserverNoEventError(self):
        """
        This method runs the observer with no error event
        """

        testError2 = self.root + self.configuration.ACCEPTANCE_TEST_SCRIPTS_LOCATION + self.FILE_SEPARATOR+"testError2.txt" 
        
        parsedLineReader2 = ParsedLineReader(testError2)
        
        scripts = [testError2]
    
        peaFacade = PythonEasyAcceptFacade(self.listenerClass,scripts)
        peaFacade.addErrorListener(self.listenerClass)
        peaFacade.executeTests()
         
        script2 = Script(self.listenerClass,testError2)
        
        parsedLine2 = parsedLineReader2.getParsedLine()
        resultLine2 = script2.executeCommand(parsedLine2)

        self.assertEquals(Configuration.SUCCESS_CODE, resultLine2.getTestLineStatus())

        self.assertEquals('', self.listenerClass.getMessage())

    def testObserverWithManyError(self):
        testError3 = self.root + self.configuration.ACCEPTANCE_TEST_SCRIPTS_LOCATION + self.FILE_SEPARATOR+"testError3.txt" 
        
        parsedLineReader3 = ParsedLineReader(testError3)
        
        scripts = [testError3]
    
        peaFacade = PythonEasyAcceptFacade(self.listenerClass,scripts)
        peaFacade.addErrorListener(self.listenerClass)
        peaFacade.executeTests()
         
        script3 = Script(self.listenerClass,testError3)
        
        parsedLine = parsedLineReader3.getParsedLine()
        parsedLine2 = parsedLineReader3.getParsedLine()
        parsedLine3 = parsedLineReader3.getParsedLine()
        parsedLine4 = parsedLineReader3.getParsedLine()
        parsedLine5 = parsedLineReader3.getParsedLine()
        
        resultLine = script3.executeCommand(parsedLine)
        resultLine2 = script3.executeCommand(parsedLine2)
        resultLine3 = script3.executeCommand(parsedLine3)
        resultLine4 = script3.executeCommand(parsedLine4)
        resultLine5 = script3.executeCommand(parsedLine5)
        
        self.assertEquals(Configuration.ERROR_CODE, resultLine.getTestLineStatus())
        self.assertEquals(Configuration.ERROR_CODE, resultLine2.getTestLineStatus())
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine3.getTestLineStatus())
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine4.getTestLineStatus())
        self.assertEquals(Configuration.ERROR_CODE, resultLine5.getTestLineStatus())
        
        self.assertEquals('Was expected: 1, but got: 2 Was expected: 2, but got: 1 Was expected: Python, but got: Pyton ', self.listenerClass.getMessage())
    
if __name__ == '__main__':
    unittest.main() 
